var app = getApp();
Page(Object.assign({},{
    data:{
        options : null,
        inited : false,
        checked : true,
        info : {status:'1',taketype:'0',list:[]},
        shoplist : [],
        userlist : [],
        sharedata : {},
        isshoppay : false,
        currentstart : new Date().getTime() + 15*60*1000,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onShow(){
        var self = this;
        wx.getClipboardData({
            success (res){
                if( res.data ){
                    var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
                    if( reg.test(res.data) ){
                        
                        app.com.confirm('检测到手机号，是否使用手机号搜索用户？',function(){
                            self.setData({
                                'info.tel' : res.data
                            })
                            self.searchtel();
                        },false,'取消','使用');

                    }
                }
            }
        })
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        var shopid = options.shopid ? options.shopid : 0;

        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addorderstart',shopid:shopid},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        shoplist : res.data.data.shoplist ? res.data.data.shoplist : [],
                        sharedata : res.data.data.sharedata,
                        params : res.data.data.params,
                        inited : true,
                        'info.shopid' : res.data.data.shopid ? res.data.data.shopid : '',
                        'info.shopname' : res.data.data.shopname ? res.data.data.shopname : '',
                        'info.status' : res.data.data.isshoppay == 1 ? '1' : 0,
                        isshoppay : res.data.data.isshoppay ? res.data.data.isshoppay : false,
                    });
                    wx.setNavigationBarTitle({
                        title: '添加订单',
                    });

                }else{
                    app.com.alert( res.data.message,function(){
                        wx.navigateBack();
                    });
                }
            });
        });
    },
    pub(){
        var self = this;
        self.savedata();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '添加订单',
            path: '',
            imageUrl : this.data.sharedata.img,
        }
    },
    savedata(){
        var self = this;
        self.data.info.ispub = self.data.checked ? 1 : 0;
        var form = JSON.stringify( self.data.info );
        app.com.http('admin','POST',{op:'addorder',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message,'success',function(){
                    wx.navigateBack();
                });
            }else{
                app.com.toast(res.data.message);
            }
        });
    },
    searchtel(){
        var self = this;
        var postdata = {
            op : 'searchuser',
            tel : this.data.info.tel
        };

        if(!postdata.tel){
            app.com.toast('先填写电话');
            return false;
        }
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                self.setData({
                    isshowuser : true,
                    userlist : res.data.data.list,
                })
            }else{
                app.com.toast( res.data.message);
            }
        });
    },
    selectuser(e){
        var i = e.currentTarget.dataset.i;
        var item = this.data.userlist[i];
        this.setData({
            'info.name' : item.name ? item.name : (item.nickname ? item.nickname : ''),
            'info.uid' : item.id,
            'info.address' : item.address ? item.address : '',
            isshowuser : false,
        })
    },
    onChange(e){
        var keys = e.currentTarget.dataset.type;
        var key = 'info.'+keys;
        this.setData({
            [key] : e.detail
        })
    },
    changestatus(){
        this.setData({
            'info.status' : this.data.info.status ? 0 : 1,
        })
    },
    onClose(){
        this.setData({
            isshowuser : false,
            showshop : false,
            showgoods : false,
            showstart : false,
        })
    },
    showshopfn(e){
        if(e.currentTarget.dataset.type == 1){
            this.setData({
                showshop : true,
            })
        }else{
            this.setData({
                showgoods : true,
            })
        }
    },
    selectshop(e){
        var i = e.currentTarget.dataset.i;
        var item = this.data.shoplist[i];
        this.setData({
            'info.shopname' : item.name ? item.name : '',
            'info.shopid' : item.id,
            'info.gid' : 0,
            'info.gtitle' : '',
            showshop : false,
            isshoppay : item.isshoppay == 1 ? 1 : 0,
        })
    },
    selectgoods(e){
        this.data.info.list.push({
            'gtitle' : e.detail.title ? e.detail.title : '',
            'gid' : e.detail.id,
            'fee' : e.detail.price,
            'hxtimes' :1,
        })
        this.setData({
            'info.list' : this.data.info.list
        })
    },
    showgoodsfn(){
        this.selectComponent("#goodslist").showgoodsfn(this.data.info.shopid);
    },
    delgitem(e){
        var i = e.currentTarget.dataset.i;
        this.data.info.list.splice(i,1);
        this.setData({
            'info.list' : this.data.info.list
        })
    },
    showstartfn(e){
        this.setData({
            showstart : true,
        })
    },
    confirmstart(e){
        
        var starttime = app.com.totime(e.detail);
        this.setData({
            'info.time' : starttime,
        })
        this.onClose();
    },
    deltime(){
        this.setData({
            'info.time' : '',
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))